import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SensitiveWordApi } from '#/api/system/sensitiveword';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getTags } from '#/api/system/sensitiveword';
import { DictTag } from '#/components/dict-tag';
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '敏感词',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入敏感词',
      },
    },
    {
      fieldName: 'tags',
      label: '标签',
      component: 'ApiSelect',
      rules: 'required',
      componentProps: {
        api: () => getTags(),
        allowClear: true,
        mode: 'tags',
        placeholder: '请选择标签',
      },
    },
    {
      fieldName: 'status',
      label: '是否启用',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'description',
      label: '描述',
      component: 'Textarea',
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '敏感词',
      field: 'name',
    },
    {
      label: '标签',
      field: 'tags',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '状态',
      field: 'statusName',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        }),
    },
    {
      label: '描述',
      field: 'description',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '敏感词',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入敏感词',
      },
    },
    {
      fieldName: 'tags',
      label: '标签',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入标签',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<SensitiveWordApi.SensitiveWord>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      type: 'seq',
      title: '序号',
      minWidth: 80,
    },
    {
      field: 'id',
      title: 'id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'name',
      title: '敏感词',
      minWidth: 120,
    },
    {
      field: 'tags',
      title: '标签',
      minWidth: 200,
    },
    {
      field: 'status',
      title: '是否启用',
      minWidth: 90,
      slots: { default: 'status' },
    },
    {
      field: 'description',
      title: '描述',
      minWidth: 300,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
